package com.panfeng.xcloud.scheduler.service.impl;

import com.panfeng.xcloud.boss.provider.dto.request.QuantBotDetailDTO;
import com.panfeng.xcloud.common.core.enums.ResponseStatusEnum;
import com.panfeng.xcloud.common.core.enums.UserBotStatusTypeEnum;
import com.panfeng.xcloud.common.core.exceptions.BaseBizException;
import com.panfeng.xcloud.scheduler.dao.mapper.UserBotMapperExt;
import com.panfeng.xcloud.scheduler.service.IUtilService;
import groovy.util.logging.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class UtilServiceImpl implements IUtilService {

    private static Logger log = LoggerFactory.getLogger(UtilServiceImpl.class);

    @Autowired
    private UserBotMapperExt userBotMapperExt;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    @Transactional
    public void deleteBot(String userBotKey, QuantBotDetailDTO quantBotDetailDTO) {
        //更改数据库中机器人状态为关闭
        int i = userBotMapperExt.updateByUserIdAndCoin(quantBotDetailDTO.getUserId(), quantBotDetailDTO.getDestinationCoin(), quantBotDetailDTO.getSourceCoin(),
                Integer.valueOf(UserBotStatusTypeEnum.STOP.getCode()), quantBotDetailDTO.getDirection(), Integer.valueOf(quantBotDetailDTO.getLeverRate()), Integer.valueOf(quantBotDetailDTO.getExchangeType()));
        if (i < 1) {
            throw new BaseBizException(ResponseStatusEnum.ERROR);
        }
        //清除redis中机器人
        stringRedisTemplate.delete(userBotKey);
    }

}
